<template>
	<div class="page">
		<div class="logo">
			<img class="logo-img" src="../assets/userLogo-2.png" alt="" />
			<div class="right-ic" @click="changeLanguage">
				<img src="../assets/100-1.png" alt="" />
				<span>{{$i18n.locale == 'zh' ? 'EN' : '中文'}}</span>
			</div>
		</div>
		<div class="content">
			<div class="search-wrap">
				<div class="search">
					<input type="text" v-model="search" :placeholder="$t('language.search1')" />
					<img class="ic" src="../assets/101.png" alt="" />
				</div>
			</div>
			<van-list v-model="loading" :finished="finished" :finished-text="$t('language.noMore')" @load="onLoad">
				<div class="card" v-for="item in list" :key="item.id">
					<div class="user" @click="tapChecked(item)">
						<div class="select">
							<img v-if="item.id == userId" class="ic" src="../assets/112.png" alt="" />
							<img v-else class="ic not" src="../assets/114.png" alt="" />
						</div>
						<div class="user-img">
							<img :src="item.avatar" alt="" />
						</div>
						<div class="user-left">
							<div class="user-name">
								{{$i18n.locale == 'zh' ? item.english_name : item.name}}
								<span class="tag">{{item.position}}</span>
							</div>
							<div class="user-text" v-if="item.hotel">{{$i18n.locale == 'zh' ? item.hotel.title : item.hotel.title_en }}
							</div>
						</div>
					</div>
				</div>
			</van-list>
		</div>

		<div class="add-btn">
			<div class="btn" @click="tapAssignLnr">{{$t('language.text41')}}</div>
		</div>
	</div>
</template>
<script>
	import {
		getUserLists,
		assignLnr,
		beUserLists,
		beLnrTransfer
	} from '@/api/user';
	import {
		Toast
	} from "vant";
	let timer = null

	export default {
		name: 'SelectSell',
		data: function() {
			return {
				locale: 'zh',
				search: '',
				type: 2,
				userId: '',
				lnrId: [],
				list: [],
				loading: false,
				finished: false,
				page: 1,
				notTap: false
			};
		},
		created() {
			const {
				query
			} = this.$route;
			this.lnrId = query.data
			const userinfo = localStorage.getItem("userinfo")
			const {
				type
			} = JSON.parse(userinfo)
			this.type = type
		},
		mounted() {},
		watch: {
			search: function() {
				const t = this
				if (timer != null) clearTimeout(timer)

				timer = setTimeout(() => {
					timer = null
					clearTimeout(timer)
					t.page = 1
					t.list = []
					t.getUserListsData()
				}, 500)
			}
		},
		methods: {
			tapChecked(item) {
				this.userId = item.id
				this.$forceUpdate()
			},
			onLoad() {
				this.getUserListsData()
			},
			tapAssignLnr() {
				const t = this
				if (t.notTap || t.userId == '') return

				t.notTap = true
				timer = setTimeout(() => {
					t.notTap = false
					clearTimeout(timer)
					timer = null
				}, 1000)

				if (this.type !== 4) {
					assignLnr({
						lnr_ids: t.lnrId.join(','),
						to_user_id: t.userId
					}).then((res) => {
						if (res.status_code != 200) {
							Toast(res.message)
						} else {
							Toast({
								duration: 2000,
								forbidClick: true,
								message: t.$i18n.tc('language.text68'),
							})
							setTimeout(() => {
								t.$router.back()
							}, 2000)
						}
					}).catch((err) => {
						console.log(err)
					})
					return
				}
				beLnrTransfer({
					be_lnr_ids: t.lnrId.join(','),
					to_user_id: t.userId
				}).then((res) => {
					if (res.status_code != 200) {
						Toast(res.message)
					} else {
						Toast({
							duration: 2000,
							forbidClick: true,
							message: t.$i18n.tc('language.text68'),
						})
						setTimeout(() => {
							t.$router.back()
						}, 2000)
					}
				}).catch((err) => {
					console.log(err)
				})
			},

			getUserListsData() {
				if (this.type !== 4) {
					console.log(111)
					getUserLists({
						page: this.page,
						key: this.search,
						is_enabled: 1
					}).then((res) => {
						let {
							current_page,
							last_page,
							data
						} = res.data
						// 加载状态结束
						this.loading = false;
						// 数据全部加载完成
						if (current_page == last_page) {
							this.finished = true;
						} else {
							this.finished = false;
							this.page++
						}
						this.list = [...this.list, ...data]
					}).catch((err) => {
						this.loading = false;
						console.log(err)
					})
					return
				}
				console.log(2222)
				beUserLists({
					page: this.page,
					key: this.search,
					is_enabled: 1
				}).then((res) => {
					let {
						current_page,
						last_page,
						data
					} = res.data
					// 加载状态结束
					this.loading = false;
					// 数据全部加载完成
					if (current_page == last_page) {
						this.finished = true;
					} else {
						this.finished = false;
						this.page++
					}
					this.list = [...this.list, ...data]
				}).catch((err) => {
					this.loading = false;
					console.log(err)
				})
			},
			goPage(page, id) {
				this.$router.push({
					path: page,
					query: {
						id
					}
				})
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					this.$cookies.set('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					this.$cookies.set('locale', 'zh')
				}
			}
		}
	};
</script>
<style type="text/css" lang="scss" scoped>
	.page {
		background-color: #f2f5f7;
	}

	.logo {
		background-color: #fff;

		.tab-app {
			display: flex;
			align-items: center;
			font-size: 24px;
			color: #333333;

			img {
				width: 31px;
				height: 30px;
				margin-right: 6px;
			}
		}

		.right-ic {
			color: #333333;
		}
	}

	.content {
		padding-bottom: 80px;

		.search-wrap {
			padding-top: 40px;
			padding-bottom: 16px;
			margin: 0 30px;

			.search {
				position: relative;
				z-index: 10;
				padding: 0 28px;
				height: 74px;
				border: 2px solid #1f4456;
				background-color: #fff;
				box-sizing: border-box;
				border-radius: 45px;
				display: flex;
				align-items: center;
				justify-content: space-between;

				input {
					font-size: 24px;
					color: #666;
					width: 100%;
					height: 100%;
				}

				.ic {
					width: 28px;
					height: 28px;
					flex-shrink: 0;
				}
			}
		}

		.card {
			margin: 24px 30px 0;
			padding: 30px;
			box-sizing: border-box;
			border-radius: 10px;
			background-color: #fff;
			box-shadow: 0 0 24px #eee;
			position: relative;

			.user {
				display: flex;
				align-items: center;
				width: 100%;
				box-sizing: border-box;
				position: relative;

				.select {
					position: absolute;
					top: 50%;
					left: 30px;
					transform: translateY(-50%);

					.ic {
						width: 54px;
						height: 54px;
						position: absolute;
						top: 84%;
						right: 0;
						transform: translateY(-50%);

						&.not {
							width: 38px;
							height: 38px;
						}
					}
				}

				.user-left {
					color: #333333;

					.user-name {
						font-size: 36px;
						font-weight: bold;
						margin-bottom: 20px;

						.tag {
							position: relative;
							top: 4px;
							color: #E8542C;
							margin-left: 12px;
							display: inline-block;
							max-width: 300px;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							padding: 8px 12px;
							border-radius: 18px 18px 18px 0;
							font-size: 20px;
							background-color: #FBE8E2;
						}

					}

					.user-text {
						font-size: 24px;
					}
				}

				.user-img {
					width: 120px;
					height: 120px;
					flex-shrink: 0;
					margin-left: 62px;
					margin-right: 20px;
					border-radius: 50%;
					border: 1px solid #fff;
					overflow: hidden;

					img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}

	.add-btn {
		position: fixed;
		left: 50%;
		bottom: 0;
		transform: translateX(-50%);
		z-index: 1000;
		width: 100%;
		height: 114px;
		max-width: 750px !important;
		color: #fff;
		font-size: 28px;
		background-color: #fff;
		box-shadow: 0 2px 24px #eee;
		padding: 14px 30px;

		.btn {
			width: 100%;
			padding: 21px 0;
			border-radius: 10px;
			text-align: center;
			background-color: #e8542c;
		}
	}
</style>